package com.cz.nongyetongv5.activity;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.AssistPatrolBean;
import com.cz.nongyetongv5.bean.AssistPatrolItem;
import com.cz.nongyetongv5.bean.AssistPatrolListResponse;
import com.cz.nongyetongv5.bean.JsonUtil;
import com.cz.nongyetongv5.bean.PatrolListResponse;
import com.cz.nongyetongv5.bean.User;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;


public class AssistPatrolItemActivity extends FragmentActivity implements View.OnClickListener {
    private User user;
    private RelativeLayout rl_back;
    private TextView tv_title;

    RecyclerView recyclerView;
    SmartRefreshLayout refreshLayout;
    private int page = 1;
    private int size = 10;
    private String region_code;
    private String region_name;
    private String year;
    private AssistPatrolBean assistPatrolBean;
    private List<AssistPatrolItem> assistPatrolItems;
    private ListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assist_patrol_item);
        user = (User) getIntent().getSerializableExtra("user");
        region_code = getIntent().getStringExtra("region_code");
        region_name = getIntent().getStringExtra("name");
        year = getIntent().getStringExtra("year");
        rl_back = findViewById(R.id.rl_back);
        rl_back.setOnClickListener(this);

        tv_title = findViewById(R.id.tv_title);
        tv_title.setText(region_name + "汇总");
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);
        assistPatrolItems = new ArrayList<>();
        getList();
        adapter = new ListAdapter();
        View notDataView = getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) recyclerView.getParent(), false);
        adapter.setEmptyView(notDataView);
        recyclerView.setLayoutManager(new LinearLayoutManager(AssistPatrolItemActivity.this));
        recyclerView.setAdapter(adapter); //设置分页和下拉刷新
        refreshLayout.setDisableContentWhenRefresh(true);
        refreshLayout.setDisableContentWhenLoading(true);
        refreshLayout.setRefreshHeader(new ClassicsHeader(AssistPatrolItemActivity.this));
        //设置 Footer
        refreshLayout.setRefreshFooter(new ClassicsFooter(AssistPatrolItemActivity.this));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                getList();
                //                WaitDialog.ShowDialog(getActivity(),"数据加载中...");

            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (assistPatrolBean != null) {
                    if (page < assistPatrolBean.getTotal()) {
                        page++;
                        getList();
                    } else {
                        refreshLayout.finishLoadmore();
                        Toast.makeText(AssistPatrolItemActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    refreshLayout.finishLoadmore();
                    Toast.makeText(AssistPatrolItemActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                }


            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.ll_count_m) {//月巡查
                    if (assistPatrolItems.get(position).getCheckMonth() != 0) {
                        Intent intent = new Intent(AssistPatrolItemActivity.this, AssistPatrolListActivity.class);
                        intent.putExtra("user", user);
                        intent.putExtra("name", assistPatrolItems.get(position).getUsername());
                        intent.putExtra("region_code", assistPatrolItems.get(position).getRegion_code());
                        intent.putExtra("year", year);
                        intent.putExtra("type", "1");
                        intent.putExtra("status", 1);
                        startActivity(intent);
                    }else {
                        Toast.makeText(AssistPatrolItemActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                    }
                } else if (view.getId() == R.id.ll_count_y) {//年巡查
                    if (assistPatrolItems.get(position).getCheckYearCount() != 0) {
                        Intent intent = new Intent(AssistPatrolItemActivity.this, AssistPatrolListActivity.class);
                        intent.putExtra("user", user);
                        intent.putExtra("name", assistPatrolItems.get(position).getUsername());
                        intent.putExtra("region_code", assistPatrolItems.get(position).getUsername());
                        intent.putExtra("year", year);
                        intent.putExtra("type", "2");
                        intent.putExtra("status", 1);
                        startActivity(intent);
                    }else {
                        Toast.makeText(AssistPatrolItemActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;

            default:
                break;
        }
    }


    void getList() {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "check/checkStatisticsUserList.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("page", String.valueOf(page))
                .addParams("size", String.valueOf(size))
                .addParams("region_code", region_code)
                .addParams("year", year)
                .addParams("login_region", user.getKnowRegion())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(AssistPatrolItemActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.println(response);
                        closeRefresh();
                        AssistPatrolListResponse assistPatrolListResponse = JsonUtil.toObject(response, AssistPatrolListResponse.class);
                        if (assistPatrolListResponse.getCode() == 0) {
                            assistPatrolBean = assistPatrolListResponse.getData();
                            if (page == 1) {
                                assistPatrolItems.clear();
                                assistPatrolItems.addAll(assistPatrolBean.getRows());
                            } else {
                                assistPatrolItems.addAll(assistPatrolBean.getRows());
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(AssistPatrolItemActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    public void closeRefresh() {
        if (refreshLayout != null) {
            if (refreshLayout.isRefreshing()) {
                refreshLayout.finishRefresh(200);
            } else if (refreshLayout.isLoading()) {
                refreshLayout.finishLoadmore(200);
            }
        }
    }

    public class ListAdapter extends BaseQuickAdapter<AssistPatrolItem, BaseViewHolder> {
        public ListAdapter() {
            super(R.layout.assist_patrol_item_item, assistPatrolItems);
        }

        @Override
        protected void convert(BaseViewHolder helper, AssistPatrolItem item) {

            helper.setTextColor(R.id.tv_title, Color.parseColor("#313131"));
            helper.setText(R.id.tv_title, item.getRealname());
            helper.setText(R.id.tv_percent, item.getPercent());
            if (item.getCheckMonth() == 0) {
                helper.setText(R.id.tv_count_m, "--");
            } else {
                helper.setText(R.id.tv_count_m, item.getCheckMonth() + "次");
            }
            if (item.getCheckYearCount() == 0) {
                helper.setText(R.id.tv_count_y, "--");
            } else {
                helper.setText(R.id.tv_count_y, item.getCheckYearCount() + "次");
            }

            helper.addOnClickListener(R.id.ll_count_m).addOnClickListener(R.id.ll_count_y);
        }
    }


}
